package com.opengw.http;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import com.opengw.configuration.config.RouteConfig;
import com.opengw.configuration.route.Route;
import com.opengw.exception.OpenGWException;

public class HTTPRequest {
	private String url;
	private boolean doInput;
	private boolean doOutput;
	private int contentLength;
	private int connectTimeout;
	private int readTimeout;
	private HashMap<String, String> requestHeaders;
	private byte[] content;
	
	public HTTPRequest(HttpServletRequest request, Route route) throws OpenGWException{


		if(!route.getAvailableMethods().contains(request.getMethod().toUpperCase()))
			throw new OpenGWException(OpenGWException.FORBIDDEN_HTTP_METHOD, true); 
		
		if(request.getMethod().toUpperCase().contains(HTTPConstants.HTTP_METHOD_POST))
			doOutput = true;
		doInput = true;
		
		url = route.getRouteURL();
		readTimeout = route.getReadTimeOut();
		connectTimeout = route.getConnectTimeOut();
		
		requestHeaders = new HashMap<String, String>();
		Enumeration headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String key = (String) headerNames.nextElement();
			requestHeaders.put(key.toLowerCase(Locale.ENGLISH), request.getHeader(key));
		}
		
		contentLength = request.getContentLength();
		try{
			if(contentLength != -1){
				content = new byte[contentLength];
				int len = 0;
				while((len = request.getInputStream().read(content, len, contentLength - len)) != -1);
			} 
		} catch (Exception e) {
			//TODO need to fix
			throw new OpenGWException(OpenGWException.IO_EXCEPTION);
		}
		
		for(RouteConfig config : route.getRouteConfigList()){
			if(config.isControlRequest())
				config.doControlRequest(this);
			if(config.isChangeRequest())
				config.doChangeRequest(this);
		}
	}
	
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUrl() {
		return url;
	}
	public void setDoInput(boolean doInput) {
		this.doInput = doInput;
	}
	public boolean isDoInput() {
		return doInput;
	}
	public void setDoOutput(boolean doOutput) {
		this.doOutput = doOutput;
	}
	public boolean isDoOutput() {
		return doOutput;
	}
	public void setContentLength(int contentLength) {
		this.contentLength = contentLength;
	}
	public int getContentLength() {
		return contentLength;
	}
	public void setConnectTimeout(int connectTimeout) {
		this.connectTimeout = connectTimeout;
	}
	public int getConnectTimeout() {
		return connectTimeout;
	}
	public void setReadTimeout(int readTimeout) {
		this.readTimeout = readTimeout;
	}
	public int getReadTimeout() {
		return readTimeout;
	}
	public void setRequestHeaders(HashMap<String, String> requestHeaders) {
		this.requestHeaders = requestHeaders;
	}
	public HashMap<String, String> getRequestHeaders() {
		return requestHeaders;
	}
	public void setContent(byte[] content) {
		this.content = content;
	}
	public byte[] getContent() {
		return content;
	}
	public String getRequestHeader(String key){
		return requestHeaders.get(key);
	}
	public void setRequestHeader(String key, String value){
		requestHeaders.put(key.toLowerCase(Locale.ENGLISH), value);
	}
	public void removeRequestHeader(String key){
		requestHeaders.remove(key);
	}
	
}
